package cn.iocoder.yudao.module.happycore.service;

import cn.iocoder.yudao.module.happycore.controller.admin.oxproject.vo.TrainRequest;
import cn.iocoder.yudao.module.happycore.controller.admin.oxproject.vo.TrainResponse;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class PythonTrainService {

    private static final String PYTHON_API_URL = "http://127.0.0.1:8000/train_svm";

    public TrainResponse trainDogBreedModel(String trainPath, String testPath,Long modeId) {
        RestTemplate restTemplate = new RestTemplate();

        // 构造请求体
        TrainRequest request = new TrainRequest(trainPath, testPath,modeId);

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        // 封装请求
        HttpEntity<TrainRequest> entity = new HttpEntity<>(request, headers);

        try {
            // 调用 FastAPI 接口
            ResponseEntity<TrainResponse> response = restTemplate.postForEntity(
                    PYTHON_API_URL,
                    entity,
                    TrainResponse.class
            );

            // 返回结果
            if (response.getStatusCode() == HttpStatus.OK && response.getBody() != null) {
                return response.getBody();
            } else {
                TrainResponse error = new TrainResponse();
                error.setStatus("error");
                error.setMessage("FastAPI 返回空结果或状态异常");
                return error;
            }

        } catch (Exception e) {
            e.printStackTrace();
            TrainResponse error = new TrainResponse();
            error.setStatus("error");
            error.setMessage("调用 FastAPI 出错: " + e.getMessage());
            return error;
        }
    }
}
